<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="../lib/ext/resources/css/ext-all-neptune.css"/>
    <link rel="stylesheet" type="text/css" href="../lib/ckeditor/sample.css" />
    <script type="text/javascript" src="../lib/ext/ext-all.js"></script>
    <script type="text/javascript" src="../lib/gf/common_grid.js"></script>
    <script type="text/javascript" src="../lib/gf/common_alert.js"></script>
    <script type="text/javascript" src="../lib/ckeditor/ckeditor.js"></script>
    <script type="text/javascript" src="../lib/ckeditor/sample.js"></script>
    <script type="text/javascript" src="../lib/ext_ux_ckeditor.js"></script>
</head>
<script type="text/javascript">
    Ext.Loader.setConfig({enabled: true});

    Ext.require([
        'Ext.grid.*',
        'Ext.data.*',
        'Ext.util.*',
        'Ext.toolbar.Paging',
        'Ext.ModelManager',
        'Ext.tip.QuickTipManager'
    ]);

    var dataForm,cascade_params1,cascade_params2,cascade_params3;
    var data = {};
    Ext.onReady(function () {



        //获取url中的参数，如果传入了参数_id那么进行修改；否则，添加。
        var _urlparams = Ext.urlDecode(document.URL.split("?")[document.URL.split("?").length - 1]);
        var form_code = '';
        if(_urlparams && _urlparams.form_code){
            form_code = _urlparams.form_code;
        }


        //get form start

        //保存数据
        function doAdd(){
            if(!dataForm.form.isValid()){
                Ext.Msg.alert('系统提示','请修正指出错误，检查输入是否正确!');
                return;
            }
            var params = dataForm.form.getValues();
            // console.dir(params);
            var _url = '/form/common/add?collection='+form_code;
            if(params && params._id ){
                _url = '/form/common/edit?collection='+form_code;
            }
            Ext.Ajax.request({
                url: _url,
                params : params,
                method : 'POST',
                success: function(response, opts) {
                    var data = Ext.decode(response.responseText);
                    if(data.flag == "success") {
                        alert_msg_refresh_close_win('业务处理成功！');
                    }else {
                        alert_msg('业务处理失败！');
                    }
                },
                failure: function(response, opts) {
                    alert_msg('业务处理失败！');
                }
            });

        }
//关闭窗口
        function doClose(){
            parent.closeWin();
        }


        function getForm() {

            var tbar = [
                {pressed: true,text: '保  存',handler: doAdd,icon : '../img/book.png'}
            ];


            var dataForm = new Ext.FormPanel({
                layout : 'fit',
                autoScroll : true,
                bodyStyle : 'overflow-x:visible; overflow-y:scroll',
            });
            var items = [];
            var form = {};
            form.tbar = tbar;
            form.xtype = 'form';
            form.autoScroll = true,
            form.layout = {type: 'table',columns:3};
            Ext.Ajax.request({
                url: '/form/'+form_code,
                method : 'GET',
                success: function(response, opts) {
                    var data = Ext.decode(response.responseText);
                    if(data) {
                        var field_arr = data.fields;
                        var fields = [];
                        fields.push({xtype:'textfield',name :'_id',hidden : true});
                        fields.push({xtype:'textfield',name :'_max_file_size',hidden : true,value:1500000});
                        Ext.each(field_arr, function (f, index){
                            var params = {};
                            params.xtype = f.xtype;
                            params.name = f.name;
                            params.fieldLabel = f.field_label;
                            params.allowBlank = f.allow_blank;
                            params.readOnly = f.read_only;
                            params.colspan = f.colspan;
                            params.width = f.width;
                            params.labelAlign = 'right';
                            if(f.width){
                                params.height = f.height;
                            }
                            if("radiogroup"==f.xtype) {
                                var opt_items = [];
                                var item = f.items;
                                var options = item.split('\n');

                                for(var i=0;i<options.length;i++) {
                                    var option = options[i];
                                    if(option!="") {
                                        var json =eval("("+option+")");
                                        opt_items.push(json);
                                    }
                                }
                                params.columns = [100,100];
                                params.vertical = true;
                                params.items = opt_items;
                                fields.push(params);
                            }else if("checkboxgroup"==f.xtype){
                                var opt_items = [];
                                var item = f.items;
                                var options = item.split('\n');

                                for(var i=0;i<options.length;i++) {
                                    var option = options[i];

                                    if(option!="") {
                                        var json =eval("("+option+")");
                                        opt_items.push(json);
                                    }
                                }
                                params.columns = [100,100];
                                params.vertical = true;
                                params.items = opt_items;
                                fields.push(params);
                            }else if("datefield"==f.xtype){
                                params.format = f.data_format;
                                fields.push(params);
                            }else if("filefield"==f.xtype){
                                //支持图片
                                params.colspan = 0;
                                params.name = 'attach_file';
                                var fieldset = {};
                                fieldset.xtype = "container";
                                fieldset.colspan = 3;
                                fieldset.layout = 'hbox';
                                fieldset.width = '800px';
                                fieldset.items = [params,{
                                    xtype: "button",
                                    text: "上传图片",
                                    width: 70,
                                    handler: function () {
                                        uploadFile(dataForm);
                                    }
                                },{
                                    fieldLabel: '图片id',
                                    labelAlign: 'right',
                                    labelWidth: 45,
                                    name: 'image_file_id',
                                    xtype: "textfield",
                                    readOnly : true
                                }];
                                fields.push(fieldset);
                            }else if("filefield2"==f.xtype){
                                //支持文件
                                params.xtype = "filefield";
                                params.colspan = 0;
                                params.name = 'attach_file_2';
                                var fieldset = {};
                                fieldset.xtype = "container";
                                fieldset.colspan = 3;
                                fieldset.layout = 'hbox';
                                fieldset.width = '800px';
                                fieldset.items = [params,{
                                    xtype: "button",
                                    text: "上传文件",
                                    width: 70,
                                    handler: function () {
                                        uploadFile2(dataForm);
                                    }
                                },{
                                    fieldLabel: '文件id',
                                    labelAlign: 'right',
                                    labelWidth: 45,
                                    name: 'attach_file_id',
                                    xtype: "textfield",
                                    readOnly : true
                                }];
                                fields.push(fieldset);
                            }else if("combobox"==f.xtype){
                                params.autoRender = true;
                                params.autoShow = true;
                                params.typeAhead = true;
                                params.queryMode = 'local';
                                params.editable = false;
                                params.displayField = f.display_field;
                                params.valueField = f.value_field;
                                if(!f.use_url) {
                                    var store = {};
                                    store.xtype = 'store';
                                    var store_fields = [];
                                    store_fields.push(f.display_field);
                                    store_fields.push(f.value_field);
                                    store.fields = store_fields;
                                    var data = [];
                                    var item = f.items;
                                    var options = item.split('\n');
                                    for(var i=0;i<options.length;i++) {
                                        var option = options[i];
                                        if(option!="") {
                                            var json =eval("("+option+")");
                                            data.push(json);
                                        }
                                    }
                                    store.data = data;
                                    params.store = store;
                                }else {
                                    //远程数据源
                                    var store = {};
                                    store.xtype = 'store';
                                    var store_fields = [];
                                    store_fields.push(f.display_field);
                                    store_fields.push(f.value_field);
                                    store.fields = store_fields;
                                    store.autoLoad = true;
                                    var proxy = {
                                        type: 'ajax',
                                        url : f.url,
                                        reader: {
                                            type: 'json',
                                            root: ''
                                        }
                                    };
                                    store.proxy = proxy;
                                    params.store = store;
                                }
                                fields.push(params);
                            }else if("ckeditor"==f.xtype) {
                                params.CKConfig = {};
                                params.fieldLabel = '';
                                params.width = "100%";
                                if(!f.height)
                                    params.height = 300;//默认300高度
                                fields.push(params);
                            } else if("cascade" == f.xtype) {
                                // 多级联动
                                // first
                                params.autoRender = true;
                                params.autoShow = true;
                                params.typeAhead = true;
                                params.queryMode = 'local';
                                params.editable = false;
                                params.xtype = "combobox";
                                params.colspan = f.colspan;
                                params.width = f.width;
                                params.displayField = f.display_field;
                                params.valueField = f.value_field;
                                cascade_params1 = Ext.clone(params);
                                cascade_params1.name = f.name;
                                cascade_params1.fieldLabel = f.field_label;
                                cascade_params1.store = new Ext.data.Store({
                                    xtype : 'store',
                                    fields : [f.display_field,f.value_field],
                                    autoLoad : true,
                                    proxy : {
                                        type : 'ajax',
                                        url : f.url,
                                        reader : {
                                            type : 'json',
                                            root : ''
                                        }
                                    }                                    
                                })
                                fields.push(cascade_params1); 
                                // var cascade_params3 = null,cascade_params2=null;;

                                if(f.cascade_field2){
                                    cascade_params2 = Ext.clone(params);
                                    cascade_params2.name = f.cascade_field2;
                                    cascade_params2.fieldLabel = f.cascade_label2;
                                    cascade_params2.store = new Ext.data.Store({
                                        xtype : 'store',
                                        fields : [f.display_field,f.value_field],
                                        autoLoad : true,
                                        proxy : {
                                            type : 'ajax',
                                            url : f.url2,
                                            reader : {
                                                type : 'json',
                                                root : ''
                                            }
                                        }                                    
                                    })
                                    fields.push(cascade_params2);
                                    // 多级联动
                                    cascade_params1.listeners = {
                                        'change': function(_this, newValue, oldValue, eOpts ){
                                            if(newValue){
                                                var tmp_url = cascade_params2.store.proxy.url;
                                                tmp_url = tmp_url.substr(0,tmp_url.lastIndexOf("=")+1) + newValue;
                                                cascade_params2.store.proxy.url = tmp_url;
                                                cascade_params2.store.load();

                                                // dataForm.form.findField(f.cascade_field2).setValue("");
                                                // cascade_params3.store.load();
                                            }
                                        }
                                    }
                                }
                                cascade_params3 = Ext.clone(params);
                                if(f.cascade_field2 && f.cascade_field3){
                                    
                                    cascade_params3.name = f.cascade_field3;
                                    cascade_params3.fieldLabel = f.cascade_label3;
                                    cascade_params3.store = new Ext.data.Store({
                                        xtype : 'store',
                                        fields : [f.display_field,f.value_field],
                                        autoLoad : true,
                                        proxy : {
                                            type : 'ajax',
                                            url : f.url3,
                                            reader : {
                                                type : 'json',
                                                root : ''
                                            }
                                        }                                    
                                    })
                                    fields.push(cascade_params3);
                                    cascade_params2.listeners = {
                                        'change': function(_this, newValue, oldValue, eOpts ){
                                            if(newValue){
                                                var tmp_url = cascade_params3.store.proxy.url;
                                                tmp_url = tmp_url.substr(0,tmp_url.lastIndexOf("=")+1) + newValue;
                                                cascade_params3.store.proxy.url = tmp_url;
                                                cascade_params3.store.load();     
                                                // dataForm.form.findField(f.cascade_field3).setValue("");                                         
                                            }
                                        }
                                    }
                                }
                            } else {
                                fields.push(params);
                            }
                        });
                        // console.log(fields);
                        form.items = fields;
                    }
                    items.push(form);
                    dataForm.add(items);
                },
                failure: function(response, opts) {

                }
            });
            return dataForm;
        }





        //get form end





        dataForm = getForm();

        var viewport = new Ext.Viewport({
            layout: 'fit',
            renderTo: "mypanel",
            items: [dataForm]
        });

        if(_urlparams && _urlparams._id){
            Ext.Ajax.request({
                url: '/form/common/detail',
                params : {'_id' : _urlparams._id,'collection':form_code},
                method : 'get',
                success: function(response, opts) {
                    data =  Ext.decode(response.responseText);
                    init_form();
                },
                failure: function(response, opts) {
                    alert('加载失败......');
                    init_form();
                }
            });
        }

        function init_form(){
            if(!data){
                return ;
            }
            for(var i in data){
                if(dataForm.form.findField(i)){
                    dataForm.form.findField(i).setValue(data[i]);
                }
            }
        }
    });
</script>
<body>
<div id="mypanel" style="width:100%"></div>
</body>
</html>